package cn.shop.dys.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;

import java.util.List;

import cn.shop.dys.R;
import cn.shop.dys.bean.ProductListItem;
import cn.shop.dys.constant.CommonConstant;
import cn.shop.lib_common.CommonViewHolder;


/**
 * Created by sugar0811 on 2016/4/21.
 */
public class ProductAdapter extends BaseAdapter {
    private BitmapUtils bitmapUtils;
    private Context context;
    private List<ProductListItem> items;
    private String HOST = "http://192.168.56.1:8080/market/";
    public ProductAdapter(Context context, List<ProductListItem> items) {
        this.context = context;
        this.items = items;
        bitmapUtils = new BitmapUtils(context);
    }

    @Override
    public int getCount() {
        return items==null || items.size() == 0? 0 : items.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    /**
     * commentCount : 10
     * id : 1
     * marketPrice : 500
     * name : 韩版时尚蕾丝裙
     * pic : /images/product/detail/c3.jpg
     * price : 350
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CommonViewHolder holder = CommonViewHolder.getCommonViewHolder(convertView, context, R.layout.item_product);
        ProductListItem item = items.get(position);
        ImageView iv = holder.getIv(R.id.iv_product);
        TextView tv = holder.getTv(R.id.tv_product_title);
        TextView tv_like_count = holder.getTv(R.id.tv_like_count);
        TextView price = holder.getTv(R.id.tv_price);
        RelativeLayout view = holder.getView(R.id.rl_product_pic);
        showNewOrHot(position%2==0?true:false, view,R.drawable.new_pro);
        showNewOrHot(position%2==0?false:true, view,R.drawable.hot_pro);
        showType(position%3,holder.getIv(R.id.iv_product_type));
        bitmapUtils.display(iv, CommonConstant.HOST+"/"+item.getPic());
        tv.setText(item.getName());
        price.setText(""+item.getPrice());
        tv_like_count.setText(""+item.getCommentCount());
        return holder.convertView;
    }

    private void showType(int type,ImageView iv) {
        int drawable = -1;
        drawable = type == 1 ? R.drawable.type_phone:type == 2 ? R.drawable.type_special:R.drawable.type_reduce;
        iv.setImageResource(drawable);
    }

    private void showNewOrHot(boolean show, RelativeLayout view,int viewId) {
        if (show) {
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(viewId);
            view.addView(imageView);
        }
    }
}
